package com.ruoyi.syf.handler.escalation.strategy;

import com.ruoyi.common.constant.TimeUnit;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.syf.common.constant.EscalationDataType;
import com.ruoyi.syf.handler.escalation.EscalationStrategy;
import com.ruoyi.syf.mapper.RiskCheckTaskMapper;
import com.ruoyi.syf.service.EscalationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 隐患排查任务信息上报
 */
@Service
public class TaskEscalationStrategy implements EscalationStrategy {

    @Resource
    private EscalationService escalationService;

    @Resource
    private RiskCheckTaskMapper taskMapper;

    @Override
    public boolean escalation() {
        List<Map<String, Object>> data = taskMapper.escalation();
        data.forEach(item -> {
            String cycleUnit = (String)item.get("CHECK_CYCLE_UNIT");
            item.put("CHECK_CYCLE_UNIT", TimeUnit.getUnitByCode(cycleUnit));
            if (TimeUnit.HOUR.getCode().equals(cycleUnit)){
                String workStartTime = (String)item.get("WORK_START_TIME");
                try {
                    Date WORK_END_TIME = DateUtils.getNextDateByCycle(DateUtils.parseDate(workStartTime,DateUtils.YYYY_MM_DD_HH_MM_SS), (Integer) item.get("CHECK_CYCLE"), cycleUnit);
                    item.put("WORK_START_TIME",DateUtils.parseDate(workStartTime,DateUtils.HH_MM_SS));
                    item.put("WORK_END_TIME",DateUtils.parseDate(workStartTime,DateUtils.HH_MM_SS));
                } catch (IllegalAccessException | ParseException e) {
                    e.printStackTrace();
                }
            }else {
                item.put("WORK_START_TIME",null);
            }
        });
        return escalationService.httpEscalation(data, getEscalationType());
    }

    @Override
    public String getEscalationType() {
        return EscalationDataType.TASK.getCode();
    }

}
